let successState = 0 // 表示成功
let fialState = 1 // 表示失败

let domain = 'http:/'

// 获取茶室信息列表
exports.getchashilist = (req, res) => {
    // 代表返回的数据结构
    let resObj = { status: successState, message: '' }
  
    // 3.0 利用orm发送sql语句查询出来分页数据即可
    let sql = " SELECT cha_name,cha_loc,cha_pic,cha_opentime,cha_closetime,cha_jingdu,cha_weidu,cha_contentid FROM cha_message "
    console.log('获取图文资讯sql语句：============>', sql)
    req.db.driver.execQuery(sql, (err, datas) => {
      // 4.0 判断是否异常
      if (err) {
        resObj.status = fialState
        resObj.message = err.message
        res.end(JSON.stringify(resObj))
        return
      }
  
      // 5.0 获取数据成功
      resObj.message = datas
      res.end(JSON.stringify(resObj))
    })
  }

  // 获取茶馆评论信息
exports.getchacontent = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }

  let id = req.params.id;
  let sql = ` SELECT cha_contentid,user_name,content,head,city,drink FROM cha_content WHERE  cha_contentid = ${id} `
  console.log('获取茶馆评论数据sql语句：============>', sql)
  req.db.driver.execQuery(sql, (err, datas) => {
    // 4.0 判断是否异常
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
      return
    }

    // 5.0 获取数据成功
    resObj.message = datas
    res.end(JSON.stringify(resObj))
  })
}

  // 插入茶馆评论信息
  exports.Insertchacontent = (req, res) => {
    // 代表返回的数据结构
    let resObj = { status: successState, message: '' }
  
    // let id = req.params.id;
    const id = req.body;
    console.log(id)
    let sql = ` INSERT INTO cha_content(cha_contentid,user_name,content,head,city,drink) 
                VALUES(`
                  + id.cha_contentid +`,'`
                  + id.user_name + `','`
                  + id.content +`','`
                  + id.head +`','`
                  + id.city +`','`
                  + id.drink +`')`
    console.log('插入茶馆评论数据sql语句：============>', sql)
    req.db.driver.execQuery(sql, (err, datas) => {
      // 4.0 判断是否异常
      if (err) {
        console.log(err)
        resObj.status = fialState
        resObj.message = err.message
        res.end(JSON.stringify(resObj))
        return
      }
  
      // 5.0 获取数据成功
      resObj.message = '获取数据成功！'
      res.end(JSON.stringify(resObj))
    })
  }

    // 插入已购买的商品
    exports.InsertHadbuy = (req, res) => {
      // 代表返回的数据结构
      let resObj = { status: successState, message: '' }
    
      // let id = req.params.id;
      const id = req.body;
      console.log(id)
      let sql = ` INSERT INTO users_buy(user_name,goods_name,pic,price) 
                  VALUES('`
                    + id.name +`','`
                    + id.title + `','`
                    + id.pic.src +`','`
                    + id.sell_price +`')`
      console.log('插入已购数据sql语句：============>', sql)
      req.db.driver.execQuery(sql, (err, datas) => {
        // 4.0 判断是否异常
        if (err) {
          console.log(err)
          resObj.status = fialState
          resObj.message = err.message
          res.end(JSON.stringify(resObj))
          return
        }
    
        // 5.0 获取数据成功
        resObj.message = '获取数据成功！'
        res.end(JSON.stringify(resObj))
      })
    }

  
// 获取已购商品信息
exports.GetHadbuy = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }

  let id = req.query;
  let sql = ` SELECT * FROM users_buy WHERE  user_name = '${id.name}'`
  console.log('获取该用户已购商品的sql语句：============>', sql)
  req.db.driver.execQuery(sql, (err, datas) => {
    // 4.0 判断是否异常
    if (err) {
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
      return
    }

    // 5.0 获取数据成功
    datas.forEach(element => {
      // console.log(element.addtime)
      let year = element.addtime.getFullYear()
      let month = element.addtime.getMonth()
      let date = element.addtime.getDate()
      element.addtime = [year,month,date].join('/')
    });
    resObj.message = datas
    // console.log(datas)
    res.end(JSON.stringify(resObj))
  })
}

// 插入收藏的讯息
exports.Insertnew = (req, res) => {
  // 代表返回的数据结构
  let resObj = { status: successState, message: '' }

  // let id = req.params.id;
  const id = req.body;
  console.log(id)
  let sql = ` INSERT INTO sc_news(user_name,id,title,add_time,img_url,click) 
              VALUES('`
                + id.name +`',`
                + id.id + `,'`
                + id.title +`','`
                + id.add_time +`','`
                + id.img_url +`','`
                + id.click +`')`
  console.log('添加收藏sql语句：============>', sql)
  req.db.driver.execQuery(sql, (err, datas) => {
    // 4.0 判断是否异常
    if (err) {
      console.log(err)
      resObj.status = fialState
      resObj.message = err.message
      res.end(JSON.stringify(resObj))
      return
    }

    // 5.0 获取数据成功
    resObj.message = '获取数据成功！'
    res.end(JSON.stringify(resObj))
  })
}

// 删除收藏的讯息
exports.Removenew = (req, res) => {
   // 代表返回的数据结构
   let resObj = { status: successState, message: '' }

   // let id = req.params.id;
   const id = req.body;
   let sql = ` DELETE FROM sc_news WHERE id = ${id.id}`
   console.log('删除选中的收藏sql语句：============>', sql)
   req.db.driver.execQuery(sql, (err, datas) => {
     // 4.0 判断是否异常
     if (err) {
       console.log(err)
       resObj.status = fialState
       resObj.message = err.message
       res.end(JSON.stringify(resObj))
       return
     }
 
     // 5.0 获取数据成功
     resObj.message = '获取数据成功！'
     res.end(JSON.stringify(resObj))
   })
}

  // 获取我的该收藏信息
  exports.getmynew = (req, res) => {
    // 代表返回的数据结构
    let resObj = { status: successState, message: '' }
  
    let id = req.body;
    let sql = ` SELECT * FROM sc_news WHERE  id = ${id.id} AND user_name = '${id.name}'`
    console.log('查询我的收藏中有没有该讯息：============>', sql)
    req.db.driver.execQuery(sql, (err, datas) => {
      // 4.0 判断是否异常
      if (err) {
        resObj.status = fialState
        resObj.message = err.message
        res.end(JSON.stringify(resObj))
        return
      }
  
      // 5.0 获取数据成功
      resObj.message = datas
      res.end(JSON.stringify(resObj))
    })
  }

   // 获取全部我的该收藏信息
   exports.getAllmynew = (req, res) => {
    // 代表返回的数据结构
    let resObj = { status: successState, message: '' }
  
    let id = req.body;
    let sql = ` SELECT * FROM sc_news WHERE user_name = '${id.name}'`
    console.log('查询我的收藏中有没有该讯息：============>', sql)
    req.db.driver.execQuery(sql, (err, datas) => {
      // 4.0 判断是否异常
      if (err) {
        resObj.status = fialState
        resObj.message = err.message
        res.end(JSON.stringify(resObj))
        return
      }
  
      // 5.0 获取数据成功
      resObj.message = datas
      res.end(JSON.stringify(resObj))
    })
  }